package com.hspedu.homework;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;

/**
 * @author DL5O
 * @version 1.0
 */
@SuppressWarnings("all")
public class Homework02 {
    public static void main(String[] args) {
        ArrayList list = new ArrayList();

        //add 添加元素
        Car car1 = new Car("奥拓",12000);
        Car car2 = new Car("奥迪",220000);
        Car car3 = new Car("宝马",400000);
        Car car4 = new Car("宾利",5000000);

        list.add(car1);
        list.add(car2);
        list.add(car3);
        list.add(car4);
        System.out.println("====list====");
        output(list);

        //remove 删除元素，这里删除奥拓car1
        list.remove(car1);
        System.out.println("====删除后====");
        output(list);

        //contains 查找某个元素是否存在，这里找car1
        System.out.println("car1 奥拓是否存在:" + list.contains(car1));//false

        //size 获取集合元素个数
        System.out.println("list.size()=" + list.size());

        //isEmpty 判空
        System.out.println("list.isEmpty()=" + list.isEmpty());

        //clear 清空
        ArrayList newList = new ArrayList();
        for (int i = 0; i < list.size() ; i++) {
            newList.add(null);
        }
        Collections.copy(newList,list);
        System.out.println("====newList====");
        output(newList);
        System.out.println("====清空====");
        newList.clear();
        System.out.println(newList);//[]

        //addAll 添加多个元素
        newList.add(new Car("法拉利",6000000));
        newList.add(new Car("保时捷",7000000));

        list.addAll(newList);
        System.out.println("====添加后====");
        output(list);

        //containAll 查找多个元素是否存在
        System.out.println("list中的元素是否包含newList中的元素:" + list.containsAll(newList));
        //true

        list.removeAll(newList);
        System.out.println("====删除后====");
        output(list);


    }

    public static void output(ArrayList list){
        //增强for循环
      /*  for (Object car : list) {
            System.out.println(car);
        }*/

        //迭代器
        Iterator iterator = list.iterator();
        while(iterator.hasNext()){
            Object car = iterator.next();
            System.out.println(car);
        }
    }
}

@SuppressWarnings("all")
class Car{
    private String name;
    private double price;

    public Car(String name, double price) {
        this.name = name;
        this.price = price;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "Car{" +
                "name='" + name + '\'' +
                ", price=" + price +
                '}';
    }
}


